package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.HomePageResponse;
import com.cskaoyan.shopping.dto.PanelContentItemDto;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.service.IHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @Author: 蒋镇泽
 * @Description:
 * @Date: 2022-06-18-12:28
 */
@Service
public class IHomeServiceImpl implements IHomeService {
    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    ContentConverter contentConverter;

    @Override
    public HomePageResponse homepage() {
        return null;
    }

    @Override
    public HomePageResponse queryAll() {
        HomePageResponse response = new HomePageResponse();
        try {
            List<Panel> panels=panelMapper.queryAll();
            ArrayList<PanelDto> panelDtos = new ArrayList<>();
            for (Panel panel : panels) {
                PanelDto panelDto = contentConverter.panel2Dto(panel);
                if(panel.getProductId()!=null){
                    continue;
                }
                List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
                List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);
                panelDto.setPanelContentItems(panelContentItemDtos);
                panelDtos.add(panelDto);
            }
            response.setPanelContentItemDtos(new HashSet<>(panelDtos));
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
        }catch (Exception e){
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }

        return response;
    }
}
